/***************************************************************************
*  Editproxyservers.cpp
*
*  July 2007
*  Copyright  2007  RuijinZheng,China
*  Email mebetop@126.com
****************************************************************************/

/*
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */
#include <QMessageBox>
#include <QList>
#include <QString>
#include <QFileDialog>
#include <QFile>
#include <QIODevice>
#include <QTextStream>
#include "Editproxyservers.h"
Editproxylist::Editproxylist(QWidget *parent,Qt::WindowFlags f,bool seperate): QDialog(parent,f)
{
    setupUi(this); 
    proxyfile=QString(getenv("HOME"))+QString("/.netflea/proxyserver.list");
    connect(pushButtonAdd, SIGNAL(clicked()), this, SLOT(launchAddingDialog()));
    connect(pushButtonDel, SIGNAL(clicked()), this, SLOT(deleteItem()));
    connect(pushButtonOK, SIGNAL(clicked()), this, SLOT(saveProxyfile()));
    this->pushButtonDel->setIcon(QIcon(":/icons/listdel.png"));
    this->pushButtonDel->setIconSize(QSize(30,30));
    this->pushButtonAdd->setIcon(QIcon(":/icons/listadd.png"));
    this->pushButtonAdd->setIconSize(QSize(30,30));
   // connect(listWidget,SLOT(itemDoubleClicked(QListWidgetItem)),listWidget,SIGNAL(editItem(QListWidgetItem)));
    this->loadProxyServers();
    this->seperate=seperate;

}
Editproxylist::~Editproxylist()
{
}
void Editproxylist::launchAddingDialog()
{
	ProxyServer proxy;
   connect(&proxy,SIGNAL(toAddThisProxyServer(QString)),this, SLOT(insertItem(QString)));
	proxy.exec();
}
void Editproxylist::insertItem(QString s){
	 listWidget->addItem(s);
	// emit proxyListChanged();
}
void Editproxylist::deleteItem()
{
     QListWidgetItem * curitem = listWidget->takeItem(listWidget->currentRow());
     delete curitem;
}
void Editproxylist::saveProxyfile()
{
     QFile file(proxyfile);
     if(file.open(QIODevice::ReadWrite|QIODevice::Truncate)){
         QTextStream in(&file);
         for(int i=0;i<listWidget->count();i++){
              in<<listWidget->item(i)->text()<<"\n";
         }
         file.flush();
         file.close();
         emit this->proxyListChanged();
         QMessageBox::warning(this, tr("Information"),
              tr("Proxy list have been saved!"));
    }
    else{
          QMessageBox::warning(this, tr("Error"),
                tr("can not open file:%1 for writing")
                .arg(proxyfile));
    }
     if(this->seperate==false){
     	  emit this->rollUp();
     }else{
     	  this->accept();
     }

}
void Editproxylist::loadProxyServers(){
    QFile file(proxyfile);
    if(file.open(QIODevice::ReadOnly)){
        QTextStream in(&file);
        QString tmpstring;
        while(!(in.atEnd())) {
            tmpstring = in.readLine();
            if(!(tmpstring.startsWith("#"))){///  ignore the commentaries 
                this->listWidget->addItem(tmpstring);
             }
         }
        file.close();
    }
}
